// 订单相关接口,包括删除订单,查询订单
const express = require("express")
const router = express.Router()
// 引入连接池
const pool = require("../pool/pool")
// 引入封装后的响应
const Response = require("../utils/Response")
// 引入表单验证部分
const Joi = require("joi")
// 查询所有订单
router.get("/pet-order", (req, res) => {
 // 表单验证
 let schema = Joi.object({
   page: Joi.number().required(),
   pagesize: Joi.number().integer().max(50).required(),
 })
 let { error, value } = schema.validate(req.query)
 if (error) {
   res.send(Response.error(error))
   return
 }
 // 执行查询业务
 let { page, pagesize } = req.query
 let startIndex = (page - 1) * 10
 let size = parseInt(pagesize)
 let sql = "select * from pet_po limit ?,?"
 pool.query(sql, [startIndex, size], (error, result) => {
   if (error) {
     throw error
   }
   res.send(Response.ok(result))
 })
})
// 删除所有订单
router.post("/pet-order/del", (req, res) => {
 let { po_id } = req.body
 let schema = Joi.object({
   po_id: Joi.string().required(),
 })
 let { error, value } = schema.validate(req.body)
 if (error) {
   res.send(Response.error(400, error))
   return
 }
 let sql = "delete from pet_po where po_id=?"
 pool.query(sql, [po_id], (error, result) => {
   if (error) {
     res.send(Response.error(500, error))
     throw error
   }
   res.send(Response.ok())
 })
})

module.exports = router;